﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using WebServiceKFC.Model;
using WebServiceKFC.Utils;

namespace WebServiceKFC.MonAnF
{
    public abstract class PhanAnDTO: MonAnDTO
    {
        protected List<string> _dsMaMonAnDon = new List<string>();

        public List<string> DsMaMonAnDon
        {
            get { return _dsMaMonAnDon; }
            set { _dsMaMonAnDon = value; }
        }

        protected List<int> _dsSoLuongMon = new List<int>();

        public List<int> DsSoLuongMon
        {
            get { return _dsSoLuongMon; }
            set { _dsSoLuongMon = value; }
        }

        public override System.Xml.XmlDocument GetExtraAttributes()
        {
            return UtilsXMLDTO.PortExAttToXML(DsMaMonAnDon, DsSoLuongMon);
        }

        public void ThemMonAnDon(string ma, int SoLuong)
        {
            this.DsMaMonAnDon.Add(ma);
            this.DsSoLuongMon.Add(SoLuong);
        }

        public override void SetInforFrom(Model.MonAn i)
        {
            base.SetInforFrom(i);

            DatabaseKFCEntities db = new DatabaseKFCEntities();
            List<DSMonAnDon> list = (from d in db.DSMonAnDons
                                     where d.MaMonAn == i.MaMonAn
                                     select d).ToList();

            foreach (var item in list)
            {
                this.DsMaMonAnDon.Add(item.MaMonAnDon);
                this.DsSoLuongMon.Add((int)item.SoLuongMon);
            }
        }

        public override void SetInforFrom(SafeMonAnDTO i)
        {
            base.SetInforFrom(i);

            UtilsXMLDTO.readXML(DsMaMonAnDon, DsSoLuongMon, i.ExtraAttributes);
        }

        public override void Save(DatabaseKFCEntities db, MonAn monAn)
        {
            base.Save(db, monAn);

            for (int i = 0; i < DsMaMonAnDon.Count; i++)
            {
                DSMonAnDon dsMonAnDon = new DSMonAnDon();
                dsMonAnDon.MaMonAn = this.MaMonAn;
                dsMonAnDon.MaMonAnDon = DsMaMonAnDon[i];
                dsMonAnDon.SoLuongMon = DsSoLuongMon[i];
                db.AddToDSMonAnDons(dsMonAnDon);
                db.SaveChanges();
            }
        }
    }
}